{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 采用paddlenlp中的embedding"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from copy import copy\n",
    "from typing import Dict, List, Optional, Tuple, Union\n",
    "import numpy as np\n",
    "\n",
    "from dotenv import load_dotenv, find_dotenv\n",
    "_ = load_dotenv(find_dotenv())\n",
    "\n",
    "\n",
    "class BaseEmbeddings:\n",
    "    \"\"\"\n",
    "    Base class for embeddings\n",
    "    \"\"\"\n",
    "    def __init__(self, path: str, is_api: bool) -> None:\n",
    "        self.path = path\n",
    "        self.is_api = is_api\n",
    "    \n",
    "    def get_embedding(self, text: str, model: str) -> List[float]:\n",
    "        raise NotImplementedError\n",
    "\n",
    "    def get_embeddings(self, text: List[str], model: str) -> List[List[float]]:\n",
    "        raise NotImplementedError\n",
    "    \n",
    "    @classmethod\n",
    "    def cosine_similarity(cls, vector1: List[float], vector2: List[float]) -> float:\n",
    "        \"\"\"\n",
    "        calculate cosine similarity between two vectors\n",
    "        \"\"\"\n",
    "        dot_product = np.dot(vector1, vector2)\n",
    "        magnitude = np.linalg.norm(vector1) * np.linalg.norm(vector2)\n",
    "        if not magnitude:\n",
    "            return 0\n",
    "        return dot_product / magnitude\n",
    "    \n",
    "\n",
    "class OpenAIEmbedding(BaseEmbeddings):\n",
    "    \"\"\"\n",
    "    class for OpenAI embeddings\n",
    "    \"\"\"\n",
    "    def __init__(self, path: str = '', is_api: bool = True) -> None:\n",
    "        super().__init__(path, is_api)\n",
    "        if self.is_api:\n",
    "            from openai import OpenAI\n",
    "            self.client = OpenAI()\n",
    "            self.client.api_key = os.getenv(\"OPENAI_API_KEY\")\n",
    "            self.client.base_url = os.getenv(\"OPENAI_BASE_URL\")\n",
    "    \n",
    "    def get_embedding(self, text: str, model: str = \"text-embedding-3-large\") -> List[float]:\n",
    "        if self.is_api:\n",
    "            text = text.replace(\"\\n\", \" \")\n",
    "            return self.client.embeddings.create(input=[text], model=model).data[0].embedding\n",
    "        else:\n",
    "            raise NotImplementedError\n",
    "\n",
    "class JinaEmbedding(BaseEmbeddings):\n",
    "    \"\"\"\n",
    "    class for Jina embeddings\n",
    "    \"\"\"\n",
    "    def __init__(self, path: str = 'jinaai/jina-embeddings-v2-base-zh', is_api: bool = False) -> None:\n",
    "        super().__init__(path, is_api)\n",
    "        self._model = self.load_model()\n",
    "        \n",
    "    def get_embedding(self, text: str) -> List[float]:\n",
    "        return self._model.encode([text])[0].tolist()\n",
    "    \n",
    "    def load_model(self):\n",
    "        import torch\n",
    "        from transformers import AutoModel\n",
    "        if torch.cuda.is_available():\n",
    "            device = torch.device(\"cuda\")\n",
    "        else:\n",
    "            device = torch.device(\"cpu\")\n",
    "        model = AutoModel.from_pretrained(self.path, trust_remote_code=True).to(device)\n",
    "        return model\n",
    "\n",
    "class ZhipuEmbedding(BaseEmbeddings):\n",
    "    \"\"\"\n",
    "    class for Zhipu embeddings\n",
    "    \"\"\"\n",
    "    def __init__(self, path: str = '', is_api: bool = True, embedding_dim = 1024) -> None:\n",
    "        super().__init__(path, is_api)\n",
    "        if self.is_api:\n",
    "            from zhipuai import ZhipuAI\n",
    "            self.client = ZhipuAI(api_key=os.getenv(\"ZHIPUAI_API_KEY\")) \n",
    "        self.embedding_dim = embedding_dim\n",
    "\n",
    "\n",
    "    def get_embedding(self, text: str) -> List[float]:\n",
    "        response = self.client.embeddings.create(\n",
    "        model=\"embedding-2\",\n",
    "        input=text,\n",
    "        )\n",
    "        return response.data[0].embedding\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "from paddlenlp import Taskflow\n",
    "\n",
    "class PaddleEmbedding(BaseEmbeddings):\n",
    "    def __init__(\n",
    "            self,\n",
    "            model:str=\"rocketqa-zh-base-query-encoder\",\n",
    "            batch_size:int =1,\n",
    "            **kwargs\n",
    "\n",
    "    ) -> None:\n",
    "        self.client = Taskflow(\"feature_extraction\",model=model ,batch_size = batch_size,return_tensors='np',**kwargs)\n",
    "\n",
    "    def get_embedding(self, text: str)  -> List[float]:\n",
    "        text_embeds = self.client([text])\n",
    "        result = text_embeds[\"features\"]\n",
    "        data = result[0]\n",
    "        return data.tolist()\n",
    "\n",
    "    def get_embeddings(self, text: List[str])  -> List[List[float]]:\n",
    "        text_embeds = self.client(text)\n",
    "        result = text_embeds[\"features\"]\n",
    "        data = result\n",
    "        return data.tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "embedding = PaddleEmbedding()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0.05002579465508461,\n",
       " 0.17876456677913666,\n",
       " -0.03920114040374756,\n",
       " -0.25773388147354126,\n",
       " 0.5590400099754333,\n",
       " -0.03011620044708252,\n",
       " -0.17774274945259094,\n",
       " 0.25186994671821594,\n",
       " 0.14149263501167297,\n",
       " -0.10307693481445312,\n",
       " 0.046508852392435074,\n",
       " -0.028397087007761,\n",
       " -0.11078149825334549,\n",
       " -0.07999950647354126,\n",
       " 0.00031697750091552734,\n",
       " 0.032515719532966614,\n",
       " -0.0244218111038208,\n",
       " 0.02001810073852539,\n",
       " 0.2677673101425171,\n",
       " 0.08093968033790588,\n",
       " 0.012369967997074127,\n",
       " 0.3121291399002075,\n",
       " -0.18647229671478271,\n",
       " 0.027275685220956802,\n",
       " -0.151125967502594,\n",
       " 0.1030336394906044,\n",
       " 0.1875016689300537,\n",
       " -0.03331463038921356,\n",
       " 0.06458021700382233,\n",
       " 0.3084167242050171,\n",
       " -0.15596449375152588,\n",
       " -0.24720197916030884,\n",
       " -0.04501324146986008,\n",
       " 0.3177567720413208,\n",
       " -0.2251533418893814,\n",
       " 0.08213544636964798,\n",
       " -0.09071619063615799,\n",
       " -0.010918959975242615,\n",
       " -0.376307874917984,\n",
       " 3.258854627609253,\n",
       " -0.1362328827381134,\n",
       " -0.1020917296409607,\n",
       " 0.14859209954738617,\n",
       " 0.324101984500885,\n",
       " 0.2453329712152481,\n",
       " -0.24353721737861633,\n",
       " -0.08786250650882721,\n",
       " -0.03797750174999237,\n",
       " 0.05521102249622345,\n",
       " 0.1278780996799469,\n",
       " -0.11842800676822662,\n",
       " -0.39532721042633057,\n",
       " -0.159576416015625,\n",
       " 0.09330413490533829,\n",
       " -0.0921250581741333,\n",
       " 0.20867401361465454,\n",
       " -0.13144709169864655,\n",
       " 0.12624838948249817,\n",
       " 0.18181906640529633,\n",
       " 0.04646161198616028,\n",
       " -0.12030697613954544,\n",
       " -0.10289229452610016,\n",
       " -0.03301980718970299,\n",
       " 0.11278903484344482,\n",
       " 0.059439510107040405,\n",
       " -0.11175200343132019,\n",
       " -0.14375320076942444,\n",
       " -0.010608617216348648,\n",
       " 0.0604390949010849,\n",
       " 0.019154801964759827,\n",
       " -0.2944349944591522,\n",
       " -2.44293212890625,\n",
       " 0.061844661831855774,\n",
       " -0.04455749690532684,\n",
       " -0.04408776015043259,\n",
       " -0.21222937107086182,\n",
       " -0.0970030352473259,\n",
       " 0.36853253841400146,\n",
       " 0.021931055933237076,\n",
       " -0.17995788156986237,\n",
       " 0.04505779966711998,\n",
       " -0.277790904045105,\n",
       " -0.16101117432117462,\n",
       " -0.06442383676767349,\n",
       " 0.05053328722715378,\n",
       " 0.16348712146282196,\n",
       " -0.19745934009552002,\n",
       " -0.09096956998109818,\n",
       " 0.14244607090950012,\n",
       " 0.05638432130217552,\n",
       " -0.09995254129171371,\n",
       " 0.48422449827194214,\n",
       " -0.04794368892908096,\n",
       " -0.29415518045425415,\n",
       " -0.16019755601882935,\n",
       " -0.2682657837867737,\n",
       " -0.1809503734111786,\n",
       " 0.03310590237379074,\n",
       " 0.04747137054800987,\n",
       " -0.3351162075996399,\n",
       " -0.2656271457672119,\n",
       " -0.01997961662709713,\n",
       " -0.025857269763946533,\n",
       " 0.3093964457511902,\n",
       " -0.21550066769123077,\n",
       " 0.13243836164474487,\n",
       " 0.22741848230361938,\n",
       " 0.05859968438744545,\n",
       " -0.4295668303966522,\n",
       " -0.07164546847343445,\n",
       " 0.10665759444236755,\n",
       " -0.13542939722537994,\n",
       " 0.3700256943702698,\n",
       " 0.08346118777990341,\n",
       " 0.17533989250659943,\n",
       " -0.15228913724422455,\n",
       " 0.30543917417526245,\n",
       " 0.06345756351947784,\n",
       " 0.03698655962944031,\n",
       " 0.1176825687289238,\n",
       " -0.09871823340654373,\n",
       " -0.0447961688041687,\n",
       " -0.050741635262966156,\n",
       " 0.007119540125131607,\n",
       " -0.15704305469989777,\n",
       " -0.08685867488384247,\n",
       " 0.022346585988998413,\n",
       " -0.06654323637485504,\n",
       " -0.06524910032749176,\n",
       " -0.07594427466392517,\n",
       " 0.015807265415787697,\n",
       " 0.053831666707992554,\n",
       " -0.09517423808574677,\n",
       " 0.3335951864719391,\n",
       " -0.07893753796815872,\n",
       " -0.19259735941886902,\n",
       " 0.2879948318004608,\n",
       " 0.1578105241060257,\n",
       " 0.1678568422794342,\n",
       " -0.08540604263544083,\n",
       " 0.026556402444839478,\n",
       " 0.0064553990960121155,\n",
       " -0.2954521179199219,\n",
       " -0.08197572827339172,\n",
       " 0.32154321670532227,\n",
       " 0.10069113224744797,\n",
       " 0.24132415652275085,\n",
       " 0.10252019762992859,\n",
       " 0.010618492029607296,\n",
       " 0.05636507645249367,\n",
       " -0.12552045285701752,\n",
       " 0.16671642661094666,\n",
       " -0.0010208189487457275,\n",
       " -0.045907892286777496,\n",
       " 0.10231068730354309,\n",
       " -0.038981132209300995,\n",
       " 0.1294218897819519,\n",
       " -0.022358961403369904,\n",
       " -0.028903625905513763,\n",
       " 0.09057658910751343,\n",
       " -0.010280385613441467,\n",
       " -0.07777866721153259,\n",
       " -0.10600675642490387,\n",
       " 0.4076443612575531,\n",
       " 0.11106354743242264,\n",
       " 0.1291053593158722,\n",
       " -0.06221054494380951,\n",
       " 0.04223768413066864,\n",
       " -0.04983104392886162,\n",
       " -0.14812956750392914,\n",
       " 0.033521220088005066,\n",
       " 0.11774656176567078,\n",
       " 0.017711907625198364,\n",
       " -0.14357556402683258,\n",
       " 0.2620733678340912,\n",
       " 0.10903022438287735,\n",
       " 0.1538984775543213,\n",
       " 0.052247241139411926,\n",
       " -0.04132264479994774,\n",
       " -0.34771087765693665,\n",
       " 0.04857945814728737,\n",
       " 0.0879235714673996,\n",
       " -0.45504888892173767,\n",
       " -0.21630477905273438,\n",
       " -0.38476163148880005,\n",
       " -0.013421542942523956,\n",
       " 0.2658023238182068,\n",
       " -0.2280445545911789,\n",
       " -0.05697610601782799,\n",
       " -0.07133391499519348,\n",
       " 0.3166780471801758,\n",
       " -0.021930240094661713,\n",
       " 0.21007981896400452,\n",
       " -0.1524893343448639,\n",
       " 0.02179441973567009,\n",
       " -0.32171255350112915,\n",
       " -0.02387118898332119,\n",
       " 0.16306066513061523,\n",
       " -0.04094995558261871,\n",
       " -0.1899135708808899,\n",
       " -0.15684352815151215,\n",
       " 0.18140818178653717,\n",
       " -0.05509765446186066,\n",
       " 0.3380357027053833,\n",
       " 0.25028008222579956,\n",
       " -0.3138366937637329,\n",
       " -0.40361452102661133,\n",
       " -0.09061045199632645,\n",
       " 0.018255405128002167,\n",
       " -0.09634046256542206,\n",
       " -0.013890519738197327,\n",
       " -0.1717924177646637,\n",
       " -0.3412947952747345,\n",
       " -0.022688336670398712,\n",
       " -0.3191787898540497,\n",
       " -0.24525827169418335,\n",
       " 0.20977210998535156,\n",
       " -0.08792116492986679,\n",
       " 0.020055323839187622,\n",
       " -0.004400439560413361,\n",
       " -0.23102906346321106,\n",
       " -0.21169593930244446,\n",
       " -0.09495828300714493,\n",
       " 0.03809250146150589,\n",
       " -0.24752835929393768,\n",
       " 0.10975285619497299,\n",
       " -0.16769567131996155,\n",
       " 0.05968109145760536,\n",
       " 0.14581328630447388,\n",
       " 0.15685886144638062,\n",
       " 0.16405010223388672,\n",
       " -0.07165464758872986,\n",
       " -0.18922922015190125,\n",
       " 0.04219445586204529,\n",
       " 0.05331956595182419,\n",
       " -0.1924748420715332,\n",
       " -0.06760557740926743,\n",
       " -0.05838746950030327,\n",
       " -0.23188604414463043,\n",
       " -0.06080902740359306,\n",
       " -0.08298193663358688,\n",
       " 0.07110654562711716,\n",
       " 0.16658027470111847,\n",
       " 0.07489287853240967,\n",
       " 0.0392276756465435,\n",
       " -0.03220027685165405,\n",
       " 0.22351256012916565,\n",
       " -0.3683503270149231,\n",
       " 0.2907407283782959,\n",
       " 0.06477474421262741,\n",
       " -0.03154806047677994,\n",
       " -0.06386459618806839,\n",
       " -0.09212476015090942,\n",
       " -0.29134219884872437,\n",
       " 0.12920363247394562,\n",
       " 0.14517050981521606,\n",
       " -0.10309621691703796,\n",
       " 0.08748876303434372,\n",
       " -0.10561955720186234,\n",
       " -0.037322890013456345,\n",
       " -0.10465308278799057,\n",
       " 0.2515674829483032,\n",
       " -0.028401732444763184,\n",
       " -0.0528387725353241,\n",
       " -0.3028171956539154,\n",
       " 0.14271694421768188,\n",
       " 0.022495457902550697,\n",
       " -0.05325762927532196,\n",
       " -0.11942561715841293,\n",
       " -0.021745651960372925,\n",
       " -0.3307531774044037,\n",
       " 0.05455636978149414,\n",
       " -0.06821908056735992,\n",
       " -0.13656693696975708,\n",
       " -0.07775993645191193,\n",
       " -0.08680550754070282,\n",
       " 0.06252089887857437,\n",
       " 0.15767431259155273,\n",
       " 0.08986621350049973,\n",
       " 0.2029668390750885,\n",
       " -0.0024320632219314575,\n",
       " 0.13962148129940033,\n",
       " -0.02446562610566616,\n",
       " 0.1431138664484024,\n",
       " -0.005299519747495651,\n",
       " -0.23512707650661469,\n",
       " -0.3299778699874878,\n",
       " 0.22734114527702332,\n",
       " 0.1575583815574646,\n",
       " -0.07504092901945114,\n",
       " -0.18761226534843445,\n",
       " -0.0405757911503315,\n",
       " -0.11792410910129547,\n",
       " -0.29873043298721313,\n",
       " 0.30645549297332764,\n",
       " -0.21998295187950134,\n",
       " -0.12207620590925217,\n",
       " -0.17901723086833954,\n",
       " -0.11457216739654541,\n",
       " -0.1841736137866974,\n",
       " -0.09005393832921982,\n",
       " 0.1015082523226738,\n",
       " 0.03677854686975479,\n",
       " 0.28815844655036926,\n",
       " -0.3168770670890808,\n",
       " 0.0331738255918026,\n",
       " -0.5311971306800842,\n",
       " -0.006059050559997559,\n",
       " 0.0018025115132331848,\n",
       " 0.172485813498497,\n",
       " -0.18058811128139496,\n",
       " -0.06550182402133942,\n",
       " 0.28836023807525635,\n",
       " 0.007272807881236076,\n",
       " -0.15703585743904114,\n",
       " 0.0017517060041427612,\n",
       " -0.23057425022125244,\n",
       " 0.10174854844808578,\n",
       " 0.17319391667842865,\n",
       " 0.1401233822107315,\n",
       " -0.17474520206451416,\n",
       " -0.01042330265045166,\n",
       " 0.0705488920211792,\n",
       " 0.271772176027298,\n",
       " 0.10595862567424774,\n",
       " -0.0729740560054779,\n",
       " -0.12224677950143814,\n",
       " -0.3107704520225525,\n",
       " -0.14935842156410217,\n",
       " -0.2072875052690506,\n",
       " 0.08958233892917633,\n",
       " 0.0021142661571502686,\n",
       " 0.0729171484708786,\n",
       " 0.034276679158210754,\n",
       " -0.0315520241856575,\n",
       " -0.19305649399757385,\n",
       " -0.056525908410549164,\n",
       " -0.4106332063674927,\n",
       " 0.052504803985357285,\n",
       " 0.023932360112667084,\n",
       " -0.16190947592258453,\n",
       " -0.46669483184814453,\n",
       " -0.009179145097732544,\n",
       " -0.1231435015797615,\n",
       " -0.13448315858840942,\n",
       " 0.1584719866514206,\n",
       " 0.060539208352565765,\n",
       " -0.08232536166906357,\n",
       " -0.052902668714523315,\n",
       " -0.29570361971855164,\n",
       " 0.07727764546871185,\n",
       " -0.1676904261112213,\n",
       " 0.061134811490774155,\n",
       " -0.12435903400182724,\n",
       " -0.07017470896244049,\n",
       " -0.05695173516869545,\n",
       " 0.356650173664093,\n",
       " 0.1154579371213913,\n",
       " -0.21670320630073547,\n",
       " -0.12574468553066254,\n",
       " -0.24989986419677734,\n",
       " -0.2639623284339905,\n",
       " -0.03961990028619766,\n",
       " -0.08495759963989258,\n",
       " -0.006330870091915131,\n",
       " -0.20553013682365417,\n",
       " 0.03207485377788544,\n",
       " -0.018622664734721184,\n",
       " -0.2540561854839325,\n",
       " -0.026901457458734512,\n",
       " -0.2867636978626251,\n",
       " -0.389543741941452,\n",
       " 0.08466994762420654,\n",
       " 0.3356771469116211,\n",
       " 0.060507819056510925,\n",
       " 0.006835930049419403,\n",
       " -0.050541169941425323,\n",
       " -0.09815336763858795,\n",
       " 0.26779669523239136,\n",
       " 0.12976521253585815,\n",
       " -0.2549688220024109,\n",
       " 0.2606967091560364,\n",
       " 0.11534605920314789,\n",
       " 0.05410265922546387,\n",
       " 0.16918767988681793,\n",
       " 0.1410209983587265,\n",
       " -0.09861139953136444,\n",
       " 0.025623753666877747,\n",
       " 0.09472521394491196,\n",
       " -0.11031829565763474,\n",
       " -0.020224101841449738,\n",
       " 0.05674831569194794,\n",
       " 0.061919715255498886,\n",
       " 1.464247465133667,\n",
       " 0.04337243735790253,\n",
       " 0.1510186642408371,\n",
       " -0.4250170886516571,\n",
       " -0.31584131717681885,\n",
       " -0.018657878041267395,\n",
       " 0.020334867760539055,\n",
       " 0.09279648214578629,\n",
       " 0.06571237742900848,\n",
       " 0.19360566139221191,\n",
       " 0.19904166460037231,\n",
       " -0.15153050422668457,\n",
       " 0.23864801228046417,\n",
       " -0.09823258221149445,\n",
       " -0.019983530044555664,\n",
       " -0.23323805630207062,\n",
       " -0.025665555149316788,\n",
       " -0.05038406699895859,\n",
       " -0.10427164286375046,\n",
       " -0.13300791382789612,\n",
       " 0.11423546075820923,\n",
       " 0.007700672373175621,\n",
       " 0.1950564682483673,\n",
       " -0.25031760334968567,\n",
       " -0.18421418964862823,\n",
       " 0.0676378458738327,\n",
       " -0.17913204431533813,\n",
       " 0.2777212858200073,\n",
       " -0.1490393728017807,\n",
       " 0.0013489536941051483,\n",
       " -0.12140466272830963,\n",
       " -0.047679681330919266,\n",
       " -0.1264072209596634,\n",
       " 0.010182664729654789,\n",
       " -0.0003785043954849243,\n",
       " 0.08986768126487732,\n",
       " 0.24001212418079376,\n",
       " -0.2337869256734848,\n",
       " -0.01189221441745758,\n",
       " -0.3049059510231018,\n",
       " -0.031814973801374435,\n",
       " 0.0978604406118393,\n",
       " 0.14254513382911682,\n",
       " -0.09127047657966614,\n",
       " 0.16971996426582336,\n",
       " -0.07836990803480148,\n",
       " 0.16865910589694977,\n",
       " 0.24024513363838196,\n",
       " -0.24213194847106934,\n",
       " -0.21829691529273987,\n",
       " -0.17007583379745483,\n",
       " 0.017748773097991943,\n",
       " 0.05901320278644562,\n",
       " 0.13693827390670776,\n",
       " -0.003856585593894124,\n",
       " -0.34340840578079224,\n",
       " -0.05547273904085159,\n",
       " 0.15845638513565063,\n",
       " -0.14478527009487152,\n",
       " -0.010691363364458084,\n",
       " -0.3270704448223114,\n",
       " 0.1883712112903595,\n",
       " 0.08038796484470367,\n",
       " -0.04162135720252991,\n",
       " -0.08508303016424179,\n",
       " 0.012201853096485138,\n",
       " -0.018745429813861847,\n",
       " -0.015962909907102585,\n",
       " 0.11625660210847855,\n",
       " -0.40980014204978943,\n",
       " -0.08871099352836609,\n",
       " 0.029264315962791443,\n",
       " 0.13291727006435394,\n",
       " -0.2927861511707306,\n",
       " 0.03197558969259262,\n",
       " -0.13461902737617493,\n",
       " 0.025487318634986877,\n",
       " -0.15651841461658478,\n",
       " 0.01699211448431015,\n",
       " 0.1719028502702713,\n",
       " -0.03302576020359993,\n",
       " -0.05684657767415047,\n",
       " -0.13573962450027466,\n",
       " 0.021288838237524033,\n",
       " -0.20554187893867493,\n",
       " 0.30263203382492065,\n",
       " 0.19802318513393402,\n",
       " -0.08987309038639069,\n",
       " -0.2148004174232483,\n",
       " 0.05627068132162094,\n",
       " 0.08244317770004272,\n",
       " 0.16314508020877838,\n",
       " -0.2443941980600357,\n",
       " 0.3148743510246277,\n",
       " -0.03675580024719238,\n",
       " -0.05409392714500427,\n",
       " 0.04295136034488678,\n",
       " -0.125825434923172,\n",
       " -0.096783347427845,\n",
       " 0.02668631076812744,\n",
       " -0.43310925364494324,\n",
       " 0.03663994371891022,\n",
       " -0.20281396806240082,\n",
       " -0.029442274942994118,\n",
       " 0.14109480381011963,\n",
       " 0.022317886352539062,\n",
       " -0.11233910918235779,\n",
       " -0.12830139696598053,\n",
       " -0.21778243780136108,\n",
       " -0.319705605506897,\n",
       " 0.1916588842868805,\n",
       " 0.12288188934326172,\n",
       " 0.02770528942346573,\n",
       " -0.1764177680015564,\n",
       " 0.0033614635467529297,\n",
       " 0.020402096211910248,\n",
       " -0.06199000030755997,\n",
       " -0.10254397988319397,\n",
       " -0.10301648080348969,\n",
       " -0.11712823063135147,\n",
       " -0.21313056349754333,\n",
       " 0.21671335399150848,\n",
       " -0.20251046121120453,\n",
       " 0.11741265654563904,\n",
       " 0.015289309434592724,\n",
       " -0.18304547667503357,\n",
       " 0.021386384963989258,\n",
       " 0.04383628070354462,\n",
       " -0.0496610589325428,\n",
       " 0.25644418597221375,\n",
       " 0.06836152821779251,\n",
       " 0.27077457308769226,\n",
       " 0.1761462241411209,\n",
       " 0.01271151378750801,\n",
       " 0.34794101119041443,\n",
       " 0.054574400186538696,\n",
       " -0.039171550422906876,\n",
       " 0.2907015085220337,\n",
       " -0.09393034130334854,\n",
       " -0.06771235167980194,\n",
       " -0.1970771849155426,\n",
       " -0.15251067280769348,\n",
       " 0.036626800894737244,\n",
       " 0.2747904360294342,\n",
       " 0.09334958344697952,\n",
       " -0.3148733675479889,\n",
       " -0.353949636220932,\n",
       " 0.09171590209007263,\n",
       " 0.03770289570093155,\n",
       " 0.1397029459476471,\n",
       " 0.10406192392110825,\n",
       " -0.17974898219108582,\n",
       " -0.039937905967235565,\n",
       " -0.518218457698822,\n",
       " 0.01960495114326477,\n",
       " 0.03253791108727455,\n",
       " 0.15704357624053955,\n",
       " -0.1568998098373413,\n",
       " 0.013154730200767517,\n",
       " 0.1540268063545227,\n",
       " -0.1539246290922165,\n",
       " 0.23050570487976074,\n",
       " -0.02453012764453888,\n",
       " -0.06681406497955322,\n",
       " 0.03768238425254822,\n",
       " -0.17355848848819733,\n",
       " 0.2558053731918335,\n",
       " 0.07957808673381805,\n",
       " 0.1812456250190735,\n",
       " -0.09491369128227234,\n",
       " 0.019749850034713745,\n",
       " -0.001737549901008606,\n",
       " -0.327040433883667,\n",
       " -0.05504640191793442,\n",
       " -0.04587765410542488,\n",
       " 0.15747810900211334,\n",
       " -0.1941262036561966,\n",
       " -0.05647996440529823,\n",
       " 0.08401485532522202,\n",
       " -0.4154582619667053,\n",
       " -0.13619555532932281,\n",
       " 0.027726680040359497,\n",
       " -0.13709838688373566,\n",
       " 0.15049101412296295,\n",
       " 0.040326401591300964,\n",
       " -0.006730958819389343,\n",
       " -0.12113781273365021,\n",
       " 0.13738471269607544,\n",
       " -0.3131917119026184,\n",
       " -0.02578146755695343,\n",
       " 0.2671367824077606,\n",
       " 0.31575652956962585,\n",
       " -0.1731080859899521,\n",
       " -0.18868905305862427,\n",
       " 0.2254173308610916,\n",
       " -0.1496923863887787,\n",
       " 0.3570903241634369,\n",
       " -0.1733027547597885,\n",
       " -0.3432374894618988,\n",
       " 0.02811187505722046,\n",
       " -0.02462853491306305,\n",
       " -0.2952337861061096,\n",
       " 0.042033933103084564,\n",
       " 0.19950585067272186,\n",
       " -0.21700559556484222,\n",
       " -0.42577406764030457,\n",
       " -0.27521663904190063,\n",
       " -0.006464436650276184,\n",
       " -0.10603800415992737,\n",
       " -0.2281355857849121,\n",
       " 0.025968551635742188,\n",
       " -0.05730343610048294,\n",
       " -0.04590894281864166,\n",
       " -0.2328166663646698,\n",
       " 0.05820433795452118,\n",
       " 0.015068791806697845,\n",
       " -0.049084484577178955,\n",
       " 0.05550071597099304,\n",
       " 0.10440807044506073,\n",
       " -0.11348453909158707,\n",
       " -0.25546568632125854,\n",
       " -0.29959797859191895,\n",
       " -0.1384698748588562,\n",
       " 0.10784976184368134,\n",
       " -0.28706812858581543,\n",
       " -0.05632055550813675,\n",
       " 0.12989217042922974,\n",
       " 0.11195025593042374,\n",
       " 0.027362244203686714,\n",
       " -0.13412363827228546,\n",
       " -0.32082438468933105,\n",
       " 0.16624963283538818,\n",
       " 0.13400611281394958,\n",
       " -0.1761394441127777,\n",
       " -0.06665411591529846,\n",
       " 0.23052585124969482,\n",
       " 0.22841325402259827,\n",
       " -0.002866022288799286,\n",
       " 0.02119925618171692,\n",
       " -0.11430239677429199,\n",
       " 0.15913133323192596,\n",
       " 0.16594716906547546,\n",
       " -0.2147696614265442,\n",
       " -0.17543251812458038,\n",
       " -0.1582164168357849,\n",
       " -0.15904255211353302,\n",
       " 0.0555676594376564,\n",
       " -0.1403198391199112,\n",
       " 0.03935863822698593,\n",
       " 0.05374353379011154,\n",
       " 0.07730258256196976,\n",
       " -0.10516080260276794,\n",
       " 0.016502663493156433,\n",
       " -0.11588254570960999,\n",
       " 0.10187288373708725,\n",
       " -0.3535945415496826,\n",
       " -0.4137455224990845,\n",
       " 0.025934644043445587,\n",
       " 0.46850085258483887,\n",
       " -0.12101248651742935,\n",
       " 0.3086894154548645,\n",
       " -0.38418397307395935,\n",
       " 0.027249455451965332,\n",
       " -0.0031843483448028564,\n",
       " -0.06775300949811935,\n",
       " -0.0032519176602363586,\n",
       " 0.21551178395748138,\n",
       " 0.06892561912536621,\n",
       " -0.12694717943668365,\n",
       " 0.26836955547332764,\n",
       " 0.017987972125411034,\n",
       " 0.035180769860744476,\n",
       " -0.13340014219284058,\n",
       " 0.2771923542022705,\n",
       " -0.4294118881225586,\n",
       " -0.11712449043989182,\n",
       " -0.06491982191801071,\n",
       " -0.16567565500736237,\n",
       " -0.06777773797512054,\n",
       " 0.0008037388324737549,\n",
       " -0.1523536741733551,\n",
       " -0.008916828781366348,\n",
       " -0.12400790303945541,\n",
       " -0.15348345041275024,\n",
       " 0.011991813778877258,\n",
       " -0.33580178022384644,\n",
       " 0.31968989968299866,\n",
       " 0.14432242512702942,\n",
       " 0.03444653004407883,\n",
       " 0.15238334238529205,\n",
       " -0.04976920783519745,\n",
       " -0.1863778531551361,\n",
       " 0.002494528889656067,\n",
       " 0.14416077733039856,\n",
       " -0.15247690677642822,\n",
       " -0.016430765390396118,\n",
       " -0.0771278440952301,\n",
       " 0.0033579021692276,\n",
       " -0.10708508640527725,\n",
       " -0.16321805119514465,\n",
       " 0.024238817393779755,\n",
       " -0.03912173956632614,\n",
       " -0.007142975926399231,\n",
       " -0.14258883893489838,\n",
       " -0.11058484017848969,\n",
       " -0.06059533730149269,\n",
       " 0.007048224098980427,\n",
       " 0.2104799449443817,\n",
       " 0.004790753126144409,\n",
       " 0.2958340644836426,\n",
       " -0.15374216437339783,\n",
       " 0.5868117213249207,\n",
       " 0.5155578851699829,\n",
       " -0.5093425512313843,\n",
       " -0.13771295547485352,\n",
       " 0.1410854160785675,\n",
       " 0.16353359818458557,\n",
       " -0.21902315318584442,\n",
       " 0.13991016149520874,\n",
       " 0.16122160851955414,\n",
       " -0.19051966071128845,\n",
       " -0.21786224842071533,\n",
       " 0.12224005907773972,\n",
       " 0.05241341516375542,\n",
       " -0.1866997480392456,\n",
       " -0.028300078585743904,\n",
       " -0.08194620907306671,\n",
       " 0.14720353484153748,\n",
       " -0.11442790925502777,\n",
       " 0.04314275085926056,\n",
       " 0.06690049916505814,\n",
       " -0.04797004535794258,\n",
       " 0.14340844750404358,\n",
       " -0.03846710920333862,\n",
       " -0.025622393935918808,\n",
       " -0.01380544900894165,\n",
       " -0.02307838201522827,\n",
       " -0.08172117173671722,\n",
       " -0.06017051637172699,\n",
       " -0.0353558249771595,\n",
       " -0.11133673787117004,\n",
       " -0.06860201060771942,\n",
       " -0.05536549910902977,\n",
       " -0.5601012110710144,\n",
       " -0.09142147749662399,\n",
       " -0.2582308351993561,\n",
       " 0.3636934757232666,\n",
       " 0.14422151446342468,\n",
       " -0.10768330097198486,\n",
       " 0.3995667099952698,\n",
       " 0.06465049833059311,\n",
       " -0.20135484635829926,\n",
       " -0.06683909893035889,\n",
       " -0.04193812981247902,\n",
       " 0.11926375329494476,\n",
       " -0.0030349045991897583,\n",
       " -0.19211003184318542,\n",
       " -0.0418672040104866,\n",
       " 0.18432831764221191,\n",
       " -0.13278868794441223,\n",
       " -0.13250698149204254,\n",
       " 0.18418487906455994,\n",
       " -0.08287805318832397,\n",
       " -0.11367443948984146,\n",
       " 0.09778302907943726,\n",
       " 0.013961674645543098,\n",
       " 0.13610775768756866,\n",
       " -0.098237045109272,\n",
       " 0.1082565039396286,\n",
       " -0.3399115204811096,\n",
       " -0.03925824910402298,\n",
       " -0.13416138291358948,\n",
       " -0.06025524437427521,\n",
       " -0.10298670083284378,\n",
       " -0.12432187050580978]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "output = embedding.get_embedding(\"你好，我是程序锅\")\n",
    "output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "768"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(output)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "paddlenlp",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
